import { Component, OnInit } from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
import { HttpClient } from "@angular/common/http";
import {TranslateService} from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';
import { AuthTokenService } from '@services/auth-token.service';
import { Router, NavigationExtras,ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-deduction-order',
  templateUrl: './deduction-order.page.html',
  styleUrls: ['./deduction-order.page.scss'],
})
export class DeductionOrderPage implements OnInit {
  toggleEnabled : any;
  appUserId = this.authTokenService.getAppUserId();
  constructor( 
    private http: HttpClient,
    private authTokenService: AuthTokenService,
    private translate: TranslateService,
    private route : ActivatedRoute,
    private defaultLanguageService:DefaultLanguageService
  ) { 
    let defultLanguage = this.defaultLanguageService.getDefaultLanguage();
    this.translate.setDefaultLang(defultLanguage);
    this.translate.use(defultLanguage);
    
    this.route.data.subscribe((obj) => {
      this.movies = obj.data.responseBody;
      let item = this.movies.find(item => item.paymentMethodDisabled );

      if(item){
        this.toggleEnabled = item.paymentMethodDisabled;
        this.inner = !item.paymentMethodDisabled;
      }else{
        this.toggleEnabled = false;
        this.inner = item.paymentMethodDisabled;
      }
    });
  }

  ngOnInit() {
  }

  
  inner: boolean = true;
  
  clickToggle(evt) {
    if (evt.detail.checked) {
      this.inner = false;
    } else {
      this.inner = true;
    }

    let url = '/scm/app/disabledDeductionOrder.do';
    let requestData = {
      paymentMethodDisabled: this.toggleEnabled,
      appUserId: '123'//this.appUserId
    };
    this.http.post(url, requestData).subscribe((response:any) => {
    });
  }

  movies = [
    // 'Finshare Pay',
    // 'CITI Bank Credit Card (0112)',
    // 'Rabo Bank Debit Card (0132)'
  ];

  drop(event: CdkDragDrop<string[]>) {
    let url = '/scm/app/updateDeductionOrder.do';
    // let currentId = this.movies[event.previousIndex].id;
    moveItemInArray(this.movies, event.previousIndex, event.currentIndex);
    let currentId = this.movies[0].id;
    let requestData = {
      appUserId:'123',//  this.appUserId,
      paymentMethodDisabled:this.toggleEnabled,
      deductionOrder:{
        id:currentId
      },
      preferredPayment:true
    };

    this.http.post(url, requestData).subscribe((response) => {});
  }
}
